﻿using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aixiu.DAL
{
    /// <summary>
    /// 获取用户信息
    /// </summary>
    public class UsersDAL
    {
        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        AiXiuDbContent xiuDbContent = new AiXiuDbContent();
        public List<TBUsers> GetTBUsers()
        {
            var list = xiuDbContent.TBUsers.ToList();
            return list;
        }
        /// <summary>
        /// 通过用户编号查找
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public TBUsers FindByUserId(int userId)
        {
            return xiuDbContent.TBUsers.SingleOrDefault(m => m.Id == userId);
        }
        public TBUsers get(int id)
        {
            return xiuDbContent.TBUsers.Find(id);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public int Add(TBUsers users)
        {
            xiuDbContent.TBUsers.Add(users);
            return xiuDbContent.SaveChanges();
        }
        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            var con = xiuDbContent.TBUsers.FirstOrDefault(u => u.Id == id);
            xiuDbContent.TBUsers.Remove(con);
            return xiuDbContent.SaveChanges();
        }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public TBUsers EditWithoutAvatar(TBUsers user)
        {
            TBUsers userEntity = xiuDbContent.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.NickName = user.NickName;
            userEntity.Sex = user.Sex;
            userEntity.Birthday = user.Birthday;
            userEntity.Address = user.Address;
            userEntity.Hobby = user.Hobby;
            // 更新到数据库
            xiuDbContent.Entry(userEntity).State = EntityState.Modified;
            xiuDbContent.SaveChanges();
            // 返回结果
            return userEntity;
        }
        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public TBUsers EditAvatar(TBUsers user)
        {
            TBUsers userEntity = xiuDbContent.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.Avatar = user.Avatar;
            // 更新到数据库
            xiuDbContent.Entry(userEntity).State = EntityState.Modified;
            xiuDbContent.SaveChanges();
            // 返回结果
            return userEntity;
        }
    }
}
